package com.gitee.wsl.jvm.okhttp.intercept.transform


import okhttp3.Interceptor
import okhttp3.Response
import okhttp3.ResponseBody
import okio.buffer
import okio.source
import timber.log.Timber
import java.io.ByteArrayInputStream
import java.io.IOException
import java.io.SequenceInputStream


/**
 * @Despciption todo
 * @Author hss
 * @Date 27/07/2023 16:17
 * @Version 1.0
 */
class ModifyResponseBodyInterceptor : Interceptor {
    @Throws(IOException::class)
    override fun intercept(chain: Interceptor.Chain): Response {
        val response: Response = chain.proceed(chain.request())
        // Log.i("glide","request url--> "+chain.request().url());
        if (!response.isSuccessful) {
            return response
        }
        if (response.body == null) {
            return response
        }
        if (response.body!!.contentLength() <= 0) {
            return response
        }
        val body = response.body
        val inputStream = body?.byteStream()
        if (inputStream == null || inputStream.available() <= 0) {
            return response
        }

        //inputStream.mark(2);
        //mark(10)，那么在read()10个以内的字符时，reset（）操作后可以重新读取已经读出的数据，如果已经读取的数据超过10个，那reset()操作后，就不能正确读取以前的数据了

        //LogUtils.w("文件长度-->"+body.contentLength()+", inputStream.available():"
        //        +inputStream.available()+", "+chain.request().url()+"\nmarkSupported-"+inputStream.markSupported());
        //markSupported-false
        val read = inputStream.read()
        return if (read == dataToAdd.toInt()) {
            //隐藏文件,那么要去掉第一个字符
            Timber.w("隐藏文件,那么要去掉第一个字符-->inputStream.available():" + inputStream.available() + ", " + chain.request().url)
            response.newBuilder()
                .body(
                    ResponseBody.create(
                        body.contentType(),
                        inputStream.available().toLong(),
                        inputStream.source().buffer()
                    )
                )
                .build()
        } else {

            //LogUtils.i("非隐藏文件,正常使用-->,inputStream.reset():"+inputStream.available()+", "+chain.request().url());
            //inputStream.reset();//这里会导致死循环. 如何处理?
            val bytes = byteArrayOf(read.toByte())
            val inputStream1 = ByteArrayInputStream(bytes)
            val newInputStream = SequenceInputStream(inputStream1, inputStream)
            //newInputStream.available():1
            //LogUtils.i("非隐藏文件,正常使用2-->,newInputStream.available():"+newInputStream.available()+", "+chain.request().url());
            response.newBuilder()
                .body(
                    ResponseBody.create(
                        body.contentType(),
                        (inputStream.available() + 1).toLong(),
                        newInputStream.source().buffer()
                    )
                )
                .build()
        }
    }

    companion object {
        var dataToAdd: Byte = 0x66
    }
}